package com.fise.mwtvc

import android.content.Context
import android.os.Build
import java.io.IOException
import java.io.InputStreamReader
import java.io.LineNumberReader
import java.util.*

/**
 * @author vincent
 * @date 2022/10/17.
 * email：jiaqiangmn@gmail.com
 * description：
 */
object DeviceUtils {
    private const val TAG = "DeviceUtils"
    fun getMacAddress(): String {
        var macSerial = ""
        var str = ""
        try {
            val pp = Runtime.getRuntime().exec("cat /sys/class/net/wlan0/address");
//            val pp = Runtime.getRuntime().exec("cat /sys/class/net/eth0/address")
            val ir = InputStreamReader(pp.inputStream)
            val input = LineNumberReader(ir)
            while (null != str) {
                str = input.readLine()
                if (str != null) {
                    macSerial = str.trim { it <= ' ' } //去空格
                    break
                }
            }
            macSerial = macSerial.replace(":".toRegex(), "-").uppercase(Locale.getDefault())
        } catch (ex: IOException) {
            // 赋予默认值
            ex.printStackTrace()
        }
        return macSerial
    }

    fun getVersionCode(context: Context): Int {
        val packInfo = context.packageManager.getPackageInfo(context.packageName, 0)
        return packInfo.versionCode
    }

    fun getSn(): String {
        return try {
            val c = Class.forName("android.os.SystemProperties")
            val get = c.getMethod("get", String::class.java)
            get.invoke(c, "ro.serialno") as String
        } catch (e: Exception) {
            e.printStackTrace()
            ""
        }
    }

    fun getSystemVersion(): String {
        return Build.DISPLAY;
    }

}